package com.battcn.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.battcn.framework.mybatis.dao.impl.MybatisSqlDaoImpl;
import com.battcn.framework.springfox.SwaggerConfig;
import com.battcn.util.spring.AppContextUtil;
import com.battcn.util.upload.FileRepository;

/**
 * 配置其他bean的注解
 * @author yanglei
 * */
@Configuration 
public class otherBeanConfig {
	/*<bean id="fileRepository" class="com.battcn.util.upload.FileRepository"
			init-method="init" />*/
	@Bean(name="fileRepository",initMethod="init")
	public FileRepository getFileRepository(){
		return new FileRepository();
	}
	
	@Bean
	public AppContextUtil getAppContextUtil(){
		return new AppContextUtil();
	}
	
	@Bean(name="mybatisSqlDaoImpl")
	public MybatisSqlDaoImpl getMybatisSqlDao(){
		return new MybatisSqlDaoImpl();
	}
	/*<bean class="com.battcn.framework.springfox.SwaggerConfig" />*/
	@Bean
	public SwaggerConfig getSwaggerConfig(){
		return new SwaggerConfig();
	}
	// 该配置是为了ResponseBody转换json
	@Bean
	public HttpMessageConverters fastJsonHttpMessageConverters(){
	    //1.需要定义一个convert转换消息的对象;
	    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
	    //2:添加fastJson的配置信息;
	    FastJsonConfig fastJsonConfig = new FastJsonConfig();
	    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
	    //3处理中文乱码问题
	    List<MediaType> fastMediaTypes = new ArrayList<>();
	    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
	    //4.在convert中添加配置信息.
	    fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
	    fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
	    HttpMessageConverter<?> converter = fastJsonHttpMessageConverter;
	    return new HttpMessageConverters(converter);

	}
}
